package view;

import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import common.loadData;

import model.userModel;
import dao.encrypt;
import dao.menuDao;
import util.DocumentUtil;
import util.dataUtil;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.CallableStatement;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class login extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel lblogin = null;
	private JLabel lbuser = null;
	private JLabel lbpass = null;
	private JTextField txtuser = null;
	private JPasswordField txtpass = null;
	private JButton btnOK = null;
	private JButton btnexit = null;
	private JLabel icon = null;
	public static int[] id;
	public static int idrole=0;
	public static int IdUser;
	public login() {
		super();
		initialize();
	}

	private void initialize() {
		icon = new JLabel();
		icon.setBounds(new Rectangle(32, 10, 87, 78));
		icon.setIcon(new ImageIcon(getClass().getResource("/image/po.png")));
		icon.setText("");
		lbpass = new JLabel();
		lbpass.setBounds(new Rectangle(60, 158, 83, 32));
		lbpass.setIcon(new ImageIcon(getClass().getResource("/image/changepassword.png")));
		lbpass.setText("Password:");
		lbuser = new JLabel();
		lbuser.setBounds(new Rectangle(60, 105, 85, 30));
		lbuser.setIcon(new ImageIcon(getClass().getResource("/image/account.png")));
		lbuser.setText("Username:");
		lblogin = new JLabel();
		lblogin.setBounds(new Rectangle(127, 30, 213, 35));
		lblogin.setFont(new Font("Arial", Font.BOLD, 18));
		lblogin.setForeground(new Color(8, 110, 72));
		lblogin.setText("Login System Crimefile");
		this.setBounds(300,100,416,306);
		this.setLayout(null);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/management.png")));
		this.setResizable(false);
		this.setTitle("Login");
		this.add(lblogin, null);
		this.add(lbuser, null);
		this.add(lbpass, null);
		this.add(getTxtuser(), null);
		this.add(getTxtpass(), null);
		this.add(getBtnOK(), null);
		this.add(getBtnexit(), null);
		this.add(icon, null);
	}

	private JTextField getTxtuser() {
		if (txtuser == null) {
			txtuser = new JTextField();
			txtuser.setBounds(new Rectangle(164, 107, 171, 29));
			txtuser.setDocument(DocumentUtil.getPlainDocument(15, "^[a-z 0-9 _ -]+"));
		}
		return txtuser;
	}

	private JPasswordField getTxtpass() {
		if (txtpass == null) {
			txtpass = new JPasswordField();
			txtpass.setLocation(new Point(164, 159));
			txtpass.setSize(new Dimension(171, 29));
		}
		return txtpass;
	}

	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setBounds(new Rectangle(122, 224, 91, 32));
			btnOK.setIcon(new ImageIcon(getClass().getResource("/image/OK.png")));
			btnOK.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnOK.setHorizontalAlignment(SwingConstants.LEFT);
			btnOK.setText("OK");
			btnOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					try {
						CallableStatement cl = dataUtil.getconnConnection().prepareCall("{call sp_login(?,?,?)}");
						//2 bien truyen vao 
						cl.setString("username", txtuser.getText());
						cl.setString("password", encrypt.maHoaPw(txtpass.getText()));
						// 1 bien di ra
						cl.registerOutParameter("RESULT", java.sql.Types.INTEGER);
						cl.execute();
						Integer rs = cl.getInt("RESULT");
						 Pattern username = Pattern.compile("^[a-z0-9_-]{3,15}$");
					     Matcher mu = username.matcher(txtuser.getText());
					     boolean resultu = mu.find();
					     //pass validate
					     Pattern pa = Pattern.compile("^[a-z0-9]{6,20}$");
					     Matcher mp = pa.matcher(txtpass.getText());
					     boolean resultp = mp.find();
					      if(txtuser.getText().equals(""))
							{
								JOptionPane.showMessageDialog(null, "Enter Username!");
							}
							else if(txtpass.getText().equals(""))
							{
								JOptionPane.showMessageDialog(null, "Enter Password!");
							}
							else if(resultu==false)
							{
								JOptionPane.showMessageDialog(null, "Username wrong!");
							}
							else if(resultp==false)
							{
								JOptionPane.showMessageDialog(null, "Password wrong!");
							}
							else if(rs==1)
						    {
								ArrayList<userModel> list =menuDao.getID(txtuser.getText());
								id = new int[list.size()];
								int i=0;
								for(userModel model : list)
								{
									id[i] =model.getIdrole();
									i++;
								}
								idrole=id[0];
								IdUser=menuDao.getIDuser(txtuser.getText());
								loadData.loadDataTableUser();
								loadData.loadDataTableComplaint();
								loadData.loadDataTablePostmortem();
								loadData.datacriminal();
								loadData.dataprisoner();
								loadData.datamostwanted();
								loadData.dataFir();
								loadData.loadDataTablehis();
								(new mainForm()).setVisible(true);
								dispose();
							
							
						}
						else if(rs==2)
						{
							JOptionPane.showMessageDialog(null, "User locked, Contact Admin!");
						}
						
						else 
						{
							JOptionPane.showMessageDialog(null, "User not exist!");
						}
					} catch (HeadlessException e1) {
						e1.printStackTrace();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

				}
			});
		}
		return btnOK;
}

	private JButton getBtnexit() {
		if (btnexit == null) {
			btnexit = new JButton();
			btnexit.setText("Exit");
			btnexit.setSize(new Dimension(91, 32));
			btnexit.setHorizontalAlignment(SwingConstants.LEFT);
			btnexit.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnexit.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnexit.setLocation(new Point(240, 223));
			btnexit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
					
				}
			});
		}
		return btnexit;
	}

}
